package com.doubao.hardware.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.doubao.hardware.entity.HardwareDeviceEntity;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import java.util.List;

/**
 * 硬件设备Mapper接口
 */
@Mapper
public interface HardwareDeviceMapper extends BaseMapper<HardwareDeviceEntity> {

    /**
     * 根据MAC地址查询设备
     * @param macAddress MAC地址
     * @return 设备实体
     */
    @Select("SELECT * FROM hardware_device WHERE mac_address = #{macAddress} LIMIT 1")
    HardwareDeviceEntity selectByMacAddress(@Param("macAddress") String macAddress);

    /**
     * 根据用户ID查询设备列表
     * @param userId 用户ID
     * @return 设备列表
     */
    @Select("SELECT * FROM hardware_device WHERE user_id = #{userId}")
    List<HardwareDeviceEntity> selectByUserId(@Param("userId") Long userId);

    /**
     * 更新设备在线状态
     * @param deviceId 设备ID
     * @param isOnline 是否在线
     * @return 影响行数
     */
    @Update("UPDATE hardware_device SET is_online = #{isOnline}, last_online_time = NOW() WHERE id = #{deviceId}")
    int updateOnlineStatus(@Param("deviceId") Long deviceId, @Param("isOnline") Integer isOnline);

    /**
     * 绑定设备到用户
     * @param deviceId 设备ID
     * @param userId 用户ID
     * @return 影响行数
     */
    @Update("UPDATE hardware_device SET user_id = #{userId}, updated_at = NOW() WHERE id = #{deviceId}")
    int bindToUser(@Param("deviceId") Long deviceId, @Param("userId") Long userId);

    /**
     * 解绑设备
     * @param deviceId 设备ID
     * @return 影响行数
     */
    @Update("UPDATE hardware_device SET user_id = NULL, updated_at = NOW() WHERE id = #{deviceId}")
    int unbindDevice(@Param("deviceId") Long deviceId);


}